package com.edu.modules.baseinfo.api;

import com.alibaba.fastjson.JSONObject;
import com.edu.modules.baseinfo.service.HomeWorkService;
import com.edu.modules.baseinfo.vo.HomeWorkDetailListVo;
import com.edu.modules.baseinfo.vo.HomeWorkListVo;
import com.edu.modules.baseinfo.vo.ResultVo;
import com.sari.common.utils.RequestContextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;

/**
 * @author kwj
 * @version 2016-08-01
 */
@CrossOrigin
@Controller
@RequestMapping(value = "homework")
public class HomeWorkApi {

    @Autowired
    private HomeWorkService homeWorkService;

    @ResponseBody
    @RequestMapping(value = "save")
    public ResultVo saveTeacherHomeWork(HttpServletRequest request){

        JSONObject param = RequestContextUtils.readJson(request);
        return homeWorkService.saveTeacherHomeWork(param);
    }

    @ResponseBody
    @RequestMapping(value="/teacherHomeWorkUpload", method= RequestMethod.POST)
    public JSONObject uploadTeacherHomeWork(@RequestParam("file") MultipartFile file){

        return homeWorkService.uploadHomeWork(file);
    }

    @ResponseBody
    @RequestMapping(value="/teacherReviewVoice", method= RequestMethod.POST)
    public JSONObject uploadTeacherReviewVoice(@RequestParam("voice") MultipartFile voice){

        return homeWorkService.uploadTeacherReviewVoice(voice);
    }

    @ResponseBody
    @RequestMapping(value = "addStudentHomework", method= RequestMethod.GET)
    public ResultVo saveStudentHomework(String studentId,String homeworkId){

        return homeWorkService.saveStudentHomework(studentId,homeworkId);
    }

    @ResponseBody
    @RequestMapping(value = "studentHomeworkList")
    public HomeWorkListVo getStudentHomeworkListByCode(String code) {

        return homeWorkService.getStudentHomeworkListByCode(code);
    }

    @ResponseBody
    @RequestMapping(value = "list")
    public HomeWorkListVo getStudentHomeworkList(String studentId) {

        return homeWorkService.getStudentHomeworkList(studentId);
    }

    @ResponseBody
    @RequestMapping(value = "detail")
    public HomeWorkDetailListVo getHomeworkDetail(String studentId,String homeworkId) {

        return new HomeWorkDetailListVo(homeWorkService.getTitleUrlList(homeworkId),
                homeWorkService.getReviedHomeworkUrlList(studentId,homeworkId),homeWorkService.getHomeWorkDetail(studentId,homeworkId));
    }

    @ResponseBody
    @RequestMapping(value = "getTeacherHomeWorkByPage")
    public JSONObject getTeacherHomeWorkByPage(HttpServletRequest request) {

        JSONObject param = RequestContextUtils.readJson(request);
        return homeWorkService.getTeacherHomeWorkByPage(param);
    }

    @ResponseBody
    @RequestMapping(value = "getStudentHomeWorkByPage")
    public JSONObject getStudentHomeWorkByPage(HttpServletRequest request) {

        JSONObject param = RequestContextUtils.readJson(request);
        return homeWorkService.getStudentHomeWorkByPage(param);
    }

    @ResponseBody
    @RequestMapping(value = "delete")
    public ResultVo deleteTeacherHomeWork(String homeWorkId) {

        return homeWorkService.deleteTeacherHomeWork(homeWorkId);
    }

    @ResponseBody
    @RequestMapping(value="/studentHomeWorkSubmit",method = RequestMethod.POST)
    public ResultVo uploadHomework(HttpServletRequest request){

        JSONObject param = RequestContextUtils.readJson(request);
        return homeWorkService.saveStudentHomework(param);
    }

    @ResponseBody
    @RequestMapping(value = "review")
    public ResultVo updateStudentHomeWork(HttpServletRequest request) {

        JSONObject param = RequestContextUtils.readJson(request);
        return homeWorkService.updateStudentHomeWork(param);
    }

    @ResponseBody
    @RequestMapping(value = "studentHomeworkUrl")
    public JSONObject getStudentHomeworkUrl(String studentId,String homeworkId) {

        return homeWorkService.getStudentHomeworkUrl(studentId,homeworkId);
    }

    @ResponseBody
    @RequestMapping(value = "teacherHomeworkUrl")
    public JSONObject getTeacherHomeworkUrl(String homeworkId) {

        return homeWorkService.getTeacherHomeworkUrl(homeworkId);
    }

    @ResponseBody
    @RequestMapping(value = "deleteHomeWork")
    public ResultVo deleteHomeWork(String studentHomeworkId){

        return homeWorkService.deleteHomeWork(studentHomeworkId);
    }

    @ResponseBody
    @RequestMapping(value="/reviewedHomeWorkUpload", method= RequestMethod.POST)
    public JSONObject uploadReviewedHomeWork(@RequestParam("file") MultipartFile file){

        return homeWorkService.uploadHomeWork(file);
    }
}
